简体   繁体   English

AngularJS复制现有表行

[英]AngularJS copy existing table row

I have a table with an edit and delete button. 我有一个带有编辑和删除按钮的表。 But now I also want to make a clone button. 但是现在我也想制作一个克隆按钮。

The clone button should work as follows: It clones almost all the data (data such as the id he cannot take) from the row that the user has clicked. 克隆按钮的工作方式如下:它从用户单击的行中克隆几乎所有数据(例如他无法获取的ID之类的数据)。 Then it goes to the edit page and there it fills the data in the input/select values. 然后转到编辑页面,并在其中将数据填充到输入/选择值中。

But I have no idea how I get this done. 但是我不知道如何完成这项工作。 I have now a function which output all the data: var cloneJob = angular.extend(job); 我现在有一个输出所有数据的函数: var cloneJob = angular.extend(job);

Then it goes to the edit page location.href = '#/jobs/add'; 然后转到编辑页面location.href = '#/jobs/add'; But the problem is that he doesn't fill the input/select values. 但是问题是他没有填写输入/选择值。 Does AngularJS has a function for this? AngularJS是否有为此功能? And am I on the right track or do I need to do something else? 我是在正确的轨道上还是我需要做其他事情?

UPDATE Here is a litle bit more code: 更新这里有一点点代码:

This is my the code of my table: 这是我表的代码:

<tr ng-repeat="job in (filtered.rows = (jobs | orderBy: orderByDate:true | filter:filterByActive | filter:filter.query)) | skip:pagination.pageSkip() |limitTo:pagination.perPage" ng-class="{ inactive : !job.active }"  style="cursor: pointer;">
    <td>
        <span ng-bind="job.title"></span>
    </td>
    <td>
        <span ng-bind="job.client.name"></span>
    </td>
    <td> 
        <span ng-bind="job.referenceNumber"><span>
    </td>
    <td> 
        <span ng-bind="job.creationDate"><span>
    </td>
    <td>
        <a ng-href="#/jobs/edit/{{job.id}}/tab/candidates" ng-bind="job.candidates.length"></a>
    </td>
    <td>
        <span class="status" ng-class="job.status.value"></span>
    </td>
    <td>
        <a ng-if="job.active" ng-href="#/jobs/edit/{{job.id}}" class="icon go">
            <span class="tooltip" translate="job_name_details"></span>
        </a>
        <a ng-if="job.active" class="icon close" ng-click="showClosePopup(job)">
            <span class="tooltip" translate="job_close"></span>
        </a>

        <a ng-click="cloneJob(job)" ><span>Clone!</span></a>
        <!-- <button data-ng-click="cloneItem(food)" class="btn inline">Add</button> -->

    </td>
</tr>

Function cloneJob is: 函数cloneJob是:

$scope.cloneJob = function (job){
    var cloneJob = angular.extend(job);
    location.href = '#/jobs/add';
}

This outputs a lot of json (all the correct data) and it goes to the add page. 这会输出很多json(所有正确的数据),并转到添加页面。

Try something like 尝试类似

<tr ng-repeat="whatever in whatevers"><button ng-click="duplicateItem(whatever)">duplicate</button></tr>

And on the controller: 并在控制器上:

$scope.duplicateItem = function(item){
   $scope.duplicatedItem = angular.copy(item); //this will do a copy, not just assign a reference.
   //if you need clean the duplicate item
   delete $scope.somePropertyYouWannaClean;
}

It would better if you provided a working example fiddle or at least more code, so we can give you more accurate answers. 如果您提供一个有效的示例小提琴或至少提供更多代码,那会更好,因此我们可以为您提供更准确的答案。

Edit: 编辑:

A cleaner way would be to make the clone function load the info into a service (or factory , a singleton ). 一种更干净的方法是使clone函数将信息加载到服务 (或工厂单例 )中。 Then after loading the route you use that service to get the content back and play with it. 然后,在加载路线后,您可以使用该服务取回内容并进行播放。

Like: 喜欢:

angular.module('some.namespace.factory', [])
    .factory('CloneJobFactory', function () {
        return {
            job: null,
            loadJob: function (job) {
                var auxJob = angular.copy(job);//if you just need a shallow copy use angular.extend
                this.job =  this.cleanJob(auxJob);
            },
            getClonedJob: function(){
                return this.job;
            },
            cleanJob: function(job) {
                //code that cleans a job object that has been cloned
                delete job.propertyYouDoNotWantToKeep;

                return job;//return the cleaned job
            }
        };
    });

Then the clone function that would be in the controller (that now has to inject the factory we just made) just has to wrap the loadJob method: 然后,将在控制器中clone函数(现在必须注入我们刚刚建立的工厂 )仅必须包装 loadJob方法:

$scope.cloneJob = function (job) {
    CloneJobFactory.loadJob(job);
}

The same for the function that would use the cloned data: 使用克隆数据的功能相同:

$scope.someFunction = function (whateverParams) {
  var clonedJob = CloneJobFactory.getClonedJob();
  //whatever you want
}

This can still be improved. 这仍然可以改善。

NOTE : Angular singletons are made to, among other things, share info between controllers, services and so on. 注意 :角单例用于在控制器,服务等之间共享信息。

Make a new 'clone' route, that uses the same controller and view as your 'add' route, but passes in the id of the job that should be cloned: 创建一个新的“克隆”路由,该路由使用与“添加”路由相同的控制器并进行查看,但传入应克隆的作业的ID:

.when('/jobs/add', { templateUrl: 'jobs/add', controller: 'AddController' })
.when('/jobs/clone/:id', { templateUrl: 'jobs/add', controller: 'AddController' })

Then, in the AddController, check if an id has been passed using $routeParams. 然后,在AddController中,检查是否已使用$ routeParams传递了ID。 If there is an id, fetch the job with the id, and initialize the model by cloning the job. 如果存在ID,请使用ID提取作业,然后通过克隆作业来初始化模型。 If there's no id, initialize the model with an 'empty' job. 如果没有ID,请使用“空”作业初始化模型。

myModule.controller('AddController', function($scope, $routeParams){
    if($routeParams.id) {
        //TODO get existing job using $routeParams.id
        $scope.newJob = angular.copy(job);
    } else {
        $scope.newJob = {};
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM