Dynamic Add Row Via JQuery

本文介绍如何使用JQuery实现表格的动态添加列功能,并通过JSON格式的数据源和可配置的列名实现数据的灵活展示。

需求:本文应用主要是使用Jquery,进行Table的动态添加列。

涉及技术:JQuery、JSON、Asp.Net

1.希望数据是动态可以添加的;

2.希望数据序列列名(Name,Email)是动态的,可定制的。

最初画面:                                                 最终画面:

image    ===》image

Html 代码:

 

< table  id ="tbContent" >  
       
< tr >  
           
< td >  
               1 
           
</ td >  
       
</ tr >  
       
< tr >  
           
< td >  
               2 
           
</ td >  
       
</ tr >  
       
< tr >  
           
< td >  
               3 
           
</ td >  
       
</ tr >  
   
</ table >

 

JQuery代码:

1.希望数据是动态可以添加的

---我们采用JSON结构

因为数据可能是在HTML中直接构建,也可以从后台生成(即Entity转换成JSON),比较灵活。所以选用JSON。

以下为JSON数据:

 

var  UserList  =  [ 
   {  " UserID " 1 " Name " : {  " FirstName " " Rico " " LastName " " Rui "  },  " Email " " rico◎hotmail.com "  }, 
   {  " UserID " 2 " Name " : {  " FirstName " " XXX " " LastName " " YYY "  },  " Email " " xxx◎hotmail.com "  }, 
   {  " UserID " 3 " Name " : {  " FirstName " " ZZZ " " LastName " " AAA "  },  " Email " " YYY◎hotmail.com "  } 
               ];

 

 

 

2.希望数据序列列名(Name,Email)是动态的,可定制的。

---Ohh,这个想法不错。因为可以动态并可定制的,我们可以针对自已需求即时选用不同选项进行动态显示数据列。

如:在页面上放一个DropDownList设置 Option1:显示1-2项 、Option:显示2-4项  etc..

配置列:

 

var  list1  =  [ " Name.FirstName " " Name.LastName " ];   针对Option1的配置

var  list2  =  [ " Name.FirstName " " Name.LastName " " Email " ];   针对Option2的配置

 

以此类配。

好了,差不多。最后附上完整代码

代码:

 

< script language = " javascript "  type = " text/javascript " >  
        $( function () { 
            
var  UserList  =  [ 
                    {  " UserID " 1 " Name " : {  " FirstName " " Rico " " LastName " " Rui "  },  " Email " " rico◎hotmail.com "  }, 
                    {  " UserID " 2 " Name " : {  " FirstName " " XXX " " LastName " " YYY "  },  " Email " " xxx◎hotmail.com "  }, 
                    {  " UserID " 3 " Name " : {  " FirstName " " ZZZ " " LastName " " AAA "  },  " Email " " YYY◎hotmail.com "  } 
                    ]; 

      $( ' #tbContent ' ).find( ' tr ' ).each( function (i) { 
                
// 获取基列的tr对象 
                 var  tr  =  $( this ); 
                
// 找到第一列td的值 
                 var  tdFirstTxt  =  tr.find( " td:first " ).text(); 
                
// 获取Json中UserID的值 
                 var  userID  =  UserList[i].UserID; 
                
// 配置用于显示的List模板列 
                 var  list  =  [ " Name.FirstName " " Name.LastName " ]; 
                
// 判断第一列的td值是否与UserID的相对应 
                 if  (tdFirstTxt  ==  userID) { 
                    
// 循环List模板列 
                    $.each(list,  function (j) { 
                        
// 使用eval用于数组对象转换成JSon的对象。 
                         var  element  =  eval( " UserList[i]. "   +  list[j]); 
                        
// 动态添加列,element为动态配置(所属第一列值)其他的值 
                        tr.append( " <td> "   +  element  +   " </td> " ); 
                    }); 
                } 
             }); 
        }); 

< / script>

 

显示最终画面

image

 

Oh,还有有关于从后台生成数据转换JSON可以参见JSON IN Code.

 

 

 

转载于:https://www.cnblogs.com/RuiLei/archive/2009/11/16/1604220.html

AI Overview Implementing selective checkout with checkboxes on a cart page typically involves allowing users to select specific items in their cart for purchase, rather than requiring them to purchase all items. This functionality is not a standard feature in most e-commerce platforms and usually requires custom development or the use of specialized plugins/apps. General Approach: Modify the Cart Template: Add a checkbox next to each item in the cart. Ensure the checkbox is linked to the specific product or line item. Handle User Selections: Use JavaScript to detect changes in checkbox states (checked/unchecked). When a checkbox is selected or deselected, update the cart's subtotal and potentially the items that will be included in the checkout process. Adjust the Checkout Process: When the user proceeds to checkout, ensure that only the items corresponding to the selected checkboxes are passed to the order. This may involve modifying the platform's core checkout logic or using hooks/filters provided by the platform. Platform-Specific Considerations: WooCommerce (WordPress): Requires custom code in functions.php or a custom plugin to add checkboxes, manage selections, and modify the checkout process. You might use AJAX to update the cart dynamically as selections are made. Shopify: Involves modifying theme files (e.g., cart-template.liquid, theme.js) to add checkboxes and JavaScript to handle the logic. This often requires familiarity with Liquid (Shopify's templating language) and JavaScript. Other Platforms: The specific implementation will vary based on the platform's architecture and extensibility options. It may involve similar approaches of template modification and custom code. Important Notes: Complexity: Implementing selective checkout can be complex and may require advanced coding skills. User Experience: Carefully consider the user experience implications of selective checkout to ensure it is intuitive and does not cause confusion. Testing: Thoroughly test the functionality to ensure that selected items are correctly processed and that no loopholes exist (e.g., refreshing the page causing issues with selected items). I want to achieve this selective checkbox function in my cart page. Im using woocommerce and woodmart theme. Please advice on what to do
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值